package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.product.client.ProductFeignClient;
import com.atguigu.gmall.product.model.*;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author XIN
 * 2023/4/25
 */
@Service
@SuppressWarnings("all")
public class ItemServiceImpl implements ItemService {
	@Autowired
	private ProductFeignClient productFeignClient;
	@Autowired
	private RedissonClient redissonClient;
	
	/**
	 * 查询商品信息,汇总详情页面需要数据,远程调用商品微服务，根据skuid汇总sku商品详情页所有数据
	 *
	 * @param skuId
	 * @return
	 */
	@Override
	public Map<String, Object> getItemInfo(Long skuId) {
		RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConst.SKU_BLOOM_FILTER);
		boolean contains = bloomFilter.contains(skuId);
		if (!contains){
//			throw new RuntimeException("数据不存在");
		}
		Map<String,Object> map = new HashMap<>();
		SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
		if(skuInfo!=null){
			map.put("skuInfo", skuInfo);
		}
		BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
		if (categoryView!=null){
			map.put("categoryView", categoryView);
		}
		BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
		if (skuPrice!=null){
			map.put("price", skuPrice);
		}
		List<SpuPoster> spuPosterBySpuId = productFeignClient.getSpuPosterBySpuId(skuInfo.getSpuId());
		if(spuPosterBySpuId!=null){
			map.put("spuPosterList", spuPosterBySpuId);
		}
		List<BaseAttrInfo> attrList = productFeignClient.getAttrList(skuId);
		if(attrList!=null){
			map.put("skuAttrList", attrList);
		}
		List<SpuSaleAttr> spuSaleAttr = productFeignClient.getSpuSaleAttr(skuId, skuInfo.getSpuId());
		if(spuPosterBySpuId!=null){
			map.put("spuSaleAttrList", spuSaleAttr);
		}
		Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
		if(skuValueIdsMap!=null){
			map.put("valuesSkuJson", JSON.toJSONString(skuValueIdsMap));
		}
		return map;
	}
}
